import requests
from bs4 import BeautifulSoup
from pandas import DataFrame
import time

df = DataFrame()

s = requests.Session()
s.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0"
url = "https://sz.58.com/siji/pn"
caiwu_url = "https://sz.58.com/zpcaiwushenji/pn"


def request58(url, n=1):
    html = requests.get(url+str(n)+"/")
    html.encoding = html.apparent_encoding
    if html.status_code == 200:
        soup = BeautifulSoup(html.text, "lxml")
        return soup.select(".job_item")


def parse58(soup):
    info = {
        "address": soup.select(".address")[0].text,
        "name": soup.select(".name")[0].text,
        "cate": soup.select(".cate")[0].text,
        "xueli": soup.select(".xueli")[0].text,
        "jingyan": soup.select(".jingyan")[0].text,
        "link": soup.select(".job_name >a")[0].get("href").split("?")[0],
        "job_salary": soup.select(".job_salary")[0].text,
        # "job_wel":soup.select(".job_wel")[0].text,
        "comp_name": soup.select(".fl")[0].get("title"),
        "comp_link": soup.select(".fl")[0].get("href"),
        "sign": soup.select(".sign")[0].text,
        "date": time.strftime("%Y-%m-%d", time.localtime())
    }
    return info


for n in range(1, 71):
    info = list(map(parse58, request58(url, n)))
    df = df.append(info)
    print(info)

df.to_csv(time.strftime("%Y-%m-%d %H", time.localtime()) +
          "司机招聘.csv", index=False)
